// @ts-ignore
/* eslint-disable */
import { request } from "@/utils/request";

/** 上传音频 上传音频文件，支持单词、短语发音等类型 POST /admin/upload/audio */
export async function adminUploadAudio(
  body: {
    /** 音频类型 */
    type: "word" | "phrase";
  },
  file?: File,
  options?: { [key: string]: any }
) {
  const formData = new FormData();

  if (file) {
    formData.append("file", file);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === "object" && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ""));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.Response & { data?: API.UploadResponse }>(
    `/admin/upload/audio`,
    {
      method: "POST",
      data: formData,
      requestType: "form",
      ...(options || {}),
    }
  );
}

/** 批量上传文件 批量上传多个文件 POST /admin/upload/batch */
export async function adminUploadBatch(
  body: {
    /** 文件类型 */
    type: "card" | "topic" | "avatar" | "word" | "phrase";
  },
  files?: File,
  options?: { [key: string]: any }
) {
  const formData = new FormData();

  if (files) {
    formData.append("files", files);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === "object" && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ""));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.Response & { data?: API.BatchUploadResponse }>(
    `/admin/upload/batch`,
    {
      method: "POST",
      data: formData,
      requestType: "form",
      ...(options || {}),
    }
  );
}

/** 获取上传配置 获取文件上传的配置信息 GET /admin/upload/config */
export async function adminUploadConfig(options?: { [key: string]: any }) {
  return request<API.Response & { data?: API.UploadConfigResponse }>(
    `/admin/upload/config`,
    {
      method: "GET",
      ...(options || {}),
    }
  );
}

/** 删除文件 删除已上传的文件 DELETE /admin/upload/delete */
export async function adminUploadDelete(
  // 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
  params: API.AdminUploadDeleteParams,
  options?: { [key: string]: any }
) {
  return request<any>(`/admin/upload/delete`, {
    method: "DELETE",
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 通用文件上传 上传任意类型的文件 POST /admin/upload/file */
export async function adminUploadFile(
  body: {
    /** 文件类型标识 */
    type?: string;
  },
  file?: File,
  options?: { [key: string]: any }
) {
  const formData = new FormData();

  if (file) {
    formData.append("file", file);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === "object" && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ""));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.Response & { data?: API.UploadResponse }>(
    `/admin/upload/file`,
    {
      method: "POST",
      data: formData,
      requestType: "form",
      ...(options || {}),
    }
  );
}

/** 上传图片 上传图片文件，支持卡片、主题、头像等类型 POST /admin/upload/image */
export async function adminUploadImage(
  body: {
    /** 图片类型 */
    type: "card" | "topic" | "avatar";
  },
  file?: File,
  options?: { [key: string]: any }
) {
  const formData = new FormData();

  if (file) {
    formData.append("file", file);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === "object" && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ""));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.Response & { data?: API.UploadResponse }>(
    `/admin/upload/image`,
    {
      method: "POST",
      data: formData,
      requestType: "form",
      ...(options || {}),
    }
  );
}
